package cl.whyem.testsutilityproject.guava;

import cl.whyem.testsutilityproject.guava.dto.BaseDTO;
import cl.whyem.testsutilityproject.guava.functions.Reduce;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import java.util.ArrayList;
import java.util.List;

public class GuavaFluentIterable {

    public static List<BaseDTO> loadMockData() {
        //Aplicando 'double brace {{}}' initialization =)
        List<BaseDTO> listMap = new ArrayList<BaseDTO>() {
            {
                add(new BaseDTO() {
                    {
                        setId(1);
                        setMonto(150);
                    }
                });
                add(new BaseDTO() {
                    {
                        setId(2);
                        setMonto(50);
                    }
                });
                add(new BaseDTO() {
                    {
                        setId(3);
                        setMonto(150);
                    }
                });
            }
        };
        return listMap;
    }

    public static void applyReduce(List<BaseDTO> listMap) {
        Reduce reduce = new Reduce();
        Optional<Integer> suma = FluentIterable.from(listMap)
                .transform(reduce)
                .last();
        Integer total = suma.or(0);
        System.out.println(String.format("Function 'REDUCE' aplicado a BaseDTO(monto) => %d", total));
    }

    public static void applyReduce21(List<BaseDTO> listMap) {
        List<Integer> suma = FluentIterable.from(listMap)
                .transform(new Function<BaseDTO, Integer>() {
            public Integer apply(BaseDTO f) {
                return f.getMonto();
            }
        }).toList();

        for (Integer s : suma) {
            System.out.println(String.format("Integer %d : ", s));
        }

    }
}
